package study_elasticSearch;


import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * 学习elasticSearch
 *
 * @author 朱序豪
 * @date 2025/2/8 16:04
 */
public class CRUDIndex {
    static String BADPIG_MAPPING = "{\n" +
            "  \"mappings\": {\n" +
            "    \"properties\": {\n" +
            "      \"displayName\": {\n" +
            "        \"type\": \"text\"\n" +
            "      },\n" +
            "      \"internalName\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";



    public static void main(String[] args) {
        RestHighLevelClient client = null;
        try {
            //初始化
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "VjFqg*_dOeNkth275zoG"));

            client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
                    .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
            );

            //创建索引
            createIndex(client);

            //查询索引
            queryIndexExist(client);

            //删除索引
            deleteIndex(client);

            //查询索引
            queryIndexExist(client);

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (null != client) {
                    //关闭
                    client.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 删除索引
     */
    private static void deleteIndex(RestHighLevelClient client) throws IOException {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(Constant.INDEX);
        AcknowledgedResponse response = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        System.out.println("删除索引结果：" + response);
    }

    /**
     * 查询索引是否存在
     */
    private static void queryIndexExist(RestHighLevelClient client) throws IOException {
        GetIndexRequest getIndexRequest = new GetIndexRequest(Constant.INDEX);
        boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(exists ? "索引已存在" : "索引不存在");
    }

    /**
     * 创建索引
     */
    private static void createIndex(RestHighLevelClient client) throws IOException {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(Constant.INDEX);
        createIndexRequest.source(BADPIG_MAPPING, XContentType.JSON);
        CreateIndexResponse response = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        System.out.println("创建索引果：" + response);
    }
}
